/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.medikuak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dBasea.Abokatua;
import dBasea.Klinika;

public class KlinikaSortu extends Panela{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6331204362685316292L;
	private JLabel izena;
	private JTextField ize;
	private JLabel hiria;
	private JComboBox hiri;
	private JButton ezeztatu;
	private AbstractButton ok;
	private JTextField helbi;
	private Abokatua abok=new Abokatua();
	private Klinika klini=new Klinika();

	public KlinikaSortu(String tituloa,final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);
JPanel erdikoPanela= this.getErdikoPanela();
		
		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout(0, 0));

		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(8,1));
		
		JPanel g=new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);
		
		JPanel ge=new JPanel();
		ge.setBackground(SystemColor.activeCaption);
		erdikoa.add(ge);
		
		JPanel arrib = new JPanel();
		arrib.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arrib = new FlowLayout();
		fl_arrib.setHgap(15);
		fl_arrib.setVgap(9);
		arrib.setLayout(fl_arrib);
		izena=new JLabel("Klinika izena:");
		izena.setFont(new Font("Arial", Font.PLAIN, 14));
		ize=new JTextField(20);
		arrib.add(izena);
		arrib.add(ize);
		erdikoa.add(arrib);
		
		JPanel arribb = new JPanel();
		arribb.setBackground(SystemColor.activeCaption);
		FlowLayout arribx = new FlowLayout();
		arribx.setHgap(15);
		arribx.setVgap(10);
		arribb.setLayout(arribx);
		hiria=new JLabel("Hiria");
		arribb.add(hiria);
		hiri=new JComboBox();
		try {
			boxCarga();
		}
		catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Ezin izan da kargatu DB-tik kolore zerrenda", "Oker", JOptionPane.ERROR_MESSAGE);				
			e.printStackTrace();
		}
		hiria.setFont(new Font("Arial", Font.PLAIN, 14));
		arribb.add(hiri);
		erdikoa.add(arribb);
		
		JPanel h=new JPanel();
		h.setBackground(SystemColor.activeCaption);
		erdikoa.add(h);
		
		JLabel lblHelbidea = new JLabel("Helbidea:");
		lblHelbidea.setFont(new Font("Arial", Font.PLAIN, 14));
		h.add(lblHelbidea);
		
		helbi = new JTextField();
		h.add(helbi);
		helbi.setColumns(18);
		FlowLayout fl_behekoa = new FlowLayout();
		fl_behekoa.setVgap(20);
		fl_behekoa.setHgap(110);
		JPanel behekoa = new JPanel(fl_behekoa);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Klinika sartu");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);

		erdikoPanela.add(behekoa, BorderLayout.SOUTH);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		
		ezeztatu.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaiIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (klini.existitzenDaKlinika(helbi.getText())) {
						JOptionPane.showMessageDialog(null,
								"klinika hau existitzen da "+helbi.getText()+" helbidean \n"
										+ "Sartu klinika berria !",
								"Oker", JOptionPane.ERROR_MESSAGE);
					}

					else {
						try {
							Object hir= hiri.getSelectedItem();
							String hu=hir.toString();
								klini.klinikaSortu(ize.getText(), helbi.getText(),hu );
							JOptionPane.showMessageDialog(null,
									"Ondo sortu da Klinika",
									"", JOptionPane.INFORMATION_MESSAGE);
							eremuakGarbitu();
						}
						catch(NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null,
									"Zenbakiak sartu telefonoko eremuan",
									"Oker", JOptionPane.ERROR_MESSAGE);
						}
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	private void boxCarga() throws SQLException {
		List<String> lista = abok.getHiriak();
		hiri.removeAllItems();
		if (lista != null) {
			for (String hiria: lista) {
				hiri.addItem(hiria);
			}
		}
	}
	@Override
	public void eremuakGarbitu() {
		ize.setText("");
		helbi.setText("");
	}
	private boolean zerbaiIdatzi() {
		boolean aurki;
		aurki=ize.getText().length() > 0 && helbi.getText().length()> 0;
   		return aurki;
   	}

}
